﻿Option Strict On
Public Class MetaValue
    Public Property ApplicationName() As String
    Public Property MetaValueID() As Guid
    Public Property Notes() As String
    Public Property SystemRecord() As Boolean
    Public Property Value() As String
    Public Property ValueDescr_ALT() As String
    Public Property ValueDescr_PRM() As String
    Public Property ValueID() As String

    Public Shared Function GetMetaValueByCode(ByVal code As String) As MetaValue
        Const selectStatement As String = "SELECT MetaValueID, ApplicationName, ValueID, Value, ValueDescr_PRM, ValueDescr_ALT, Notes, SystemRecord FROM dbo.MetaValues WHERE ValueID = '{0}'"
        Return GetMetaValue(String.Format(selectStatement, code))
    End Function

    Public Shared Function GetMetaValueById(ByVal id As Guid) As MetaValue
        Const selectStatement As String = "SELECT MetaValueID, ApplicationName, ValueID, Value, ValueDescr_PRM, ValueDescr_ALT, Notes, SystemRecord FROM dbo.MetaValues WHERE MetaValueID = '{0}'"
        Return GetMetaValue(String.Format(selectStatement, id.ToString))
    End Function

    Private Shared Function GetMetaValue(ByVal selectStatement As String) As MetaValue
        Dim command As SqlCommand = Base.Connection.Command
        Dim reader As SqlDataReader = Nothing
        Dim dtrow As DataTable = New DataTable
        Dim dr As DataRow = Nothing
        '
        Dim mv As MetaValue = Nothing

        Try
            command.CommandText = selectStatement
            reader = command.ExecuteReader(CommandBehavior.CloseConnection)
            If reader.HasRows Then
                dtrow.Load(reader)
                dr = dtrow.Rows(0)
                mv = New MetaValue
                'Pk always there
                mv.MetaValueID = CType(dr("MetaValueID"), Guid)
                '
                mv.ValueID = dr("ValueID").ToString
                mv.Value = dr("Value").ToString
                mv.ValueDescr_ALT = dr("ValueDescr_ALT").ToString
                mv.ValueDescr_PRM = dr("ValueDescr_PRM").ToString
                mv.Notes = dr("Notes").ToString
                mv.SystemRecord = CBool(dr("SystemRecord"))
            End If
        Catch ex As Exception
            Throw New Exception(String.Format("Invalid SQL command: MetaValue.GetMetaValueByCode{0} {1}", vbCrLf, command.CommandText))
        Finally
            If reader IsNot Nothing Then
                reader.Close()
            End If
            dtrow.Dispose()
            command.Dispose()
        End Try
        Return mv
    End Function

End Class
